package org.jeecg.common.file;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.stream.Stream;

/**
 * 支持上传文件类型枚举
 *
 * @author jiangtianyi
 * @version 1.0
 * @date 2023-03-02
 */
public enum FileTypeEnum {

    // .txt
    TEXT_PLAIN("text/plain"),
    // .csv
    TEXT_CSV("text/csv"),
    // .rtf
    TEXT_RTF("text/rtf"),
    // .xml
    TEXT_XML("text/xml"),
    // .jp2
    IMAGE_JP2("image/jp2"),
    // .jpg / .jpe / .jpeg
    IMAGE_JPEG("image/jpeg"),
    // .dwg
    IMAGE_VND_DWG("image/vnd.dwg"),
    // .dxf
    IMAGE_VND_DXF("image/vnd.dxf"),
    // .bmp
    IMAGE_BMP("image/bmp"),
    // .png
    IMAGE_PNG("image/png"),
    // .svf
    IMAGE_VND_SVF("image/vnd.svf"),
    // .tif / .tiff
    IMAGE_TIFF("image/tiff"),
    // .gif
    IMAGE_GIF("image/gif"),
    // .3gpp / .3gp
    AUDIO_3GPP("audio/3gpp"),
    // .ac3
    AUDIO_AC3("audio/ac3"),
    // .mp2 / .mp3
    AUDIO_MPEG("audio/mpeg"),
    // .mp4
    AUDIO_MP4("audio/mp4"),
    // .au
    AUDIO_BASIC("audio/basic"),
    // .ogg
    AUDIO_OGG("audio/ogg"),
    // .3gpp / .3gp
    VIDEO_3GPP("video/3gpp"),
    // .mp2 / .mpeg / .mpg
    VIDEO_MPEG("video/mpeg"),
    // .mp4
    VIDEO_MP4("video/mp4"),
    // .mov
    VIDEO_QUICKTIME("video/quicktime"),
    // .webm
    VIDEO_WEBM("video/webm"),
    // .avi
    VIDEO_AVI("video/avi"),
    // .wmv
    VIDEO_WMV("video/x-ms-wmv"),
    // .rtf
    APPLICATION_RTF("application/rtf"),
    // .xml
    APPLICATION_XML("application/xml"),
    // .dtd
    APPLICATION_XML_DTD("application/xml-dtd"),
    // .json
    APPLICATION_JSON("application/json"),
    // .doc .dot
    APPLICATION_MSWORD("application/msword"),
    // .docx
    APPLICATION_DOCX("application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
    // .xlsx
    APPLICATION_XLSX("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
    // .xltx
    APPLICATION_XLTX("application/vnd.openxmlformats-officedocument.spreadsheetml.template"),
    // .potx
    APPLICATION_POTX("application/vnd.openxmlformats-officedocument.presentationml.template"),
    // .ppsx
    APPLICATION_PPSX("application/vnd.openxmlformats-officedocument.presentationml.slideshow"),
    // .pptx
    APPLICATION_PPTX("application/vnd.openxmlformats-officedocument.presentationml.presentation"),
    // .sldx
    APPLICATION_SLDX("application/vnd.openxmlformats-officedocument.presentationml.slide"),
    // .dotx
    APPLICATION_DOTX("application/vnd.openxmlformats-officedocument.wordprocessingml.template"),
    // .xlsm
    APPLICATION_VND_MS_EXCEL_ADDIN_XLSM("application/vnd.ms-excel.addin.macroEnabled.12"),
    // .xlsb
    APPLICATION_VND_MS_EXCEL_XLSB("application/vnd.ms-excel.sheet.binary.macroEnabled.12"),
    // .xlsm
    APPLICATION_VND_MS_EXCEL_SHEET_XLSM("application/vnd.ms-excel.sheet.macroEnabled.12"),
    // .vsdx
    APPLICATION_VND_MS_VISIO_VSDX("application/vnd.ms-visio.drawing"),
    // .pot / .pps / .ppt
    APPLICATION_VND_MS_POWERPOINT("application/vnd.ms-powerpoint"),
    // .xlc / .xlm / .xls / .xlt / .xlw
    APPLICATION_VND_MS_EXCEL("application/vnd.ms-excel"),
    // .asf
    APPLICATION_VND_MS_ASF("application/vnd.ms-asf"),
    // .mpp
    APPLICATION_MPP("application/vnd.ms-project"),
    // .wdb / .wps
    APPLICATION_VND_MS_WORKS("application/vnd.ms-works"),
    // .pdf
    APPLICATION_PDF("application/pdf"),
    // .ogg
    APPLICATION_OGG("application/ogg"),
    // .zip
    APPLICATION_ZIP("application/zip");

    /**
     * 文件类型
     */
    private final String contentType;

    FileTypeEnum(String contentType) {
        this.contentType = contentType;
    }

    public String getContentType() {
        return contentType;
    }

    /**
     * 获取文件类型枚举
     *
     * @param contentType 文件类型
     * @return 文件类型枚举
     * @author tianyi.jiang
     * @since 1.0
     */
    public static FileTypeEnum getFileTypeEnum(String contentType) {
        if (StringUtils.isNotEmpty(contentType)) {
            return Stream.of(FileTypeEnum.values())
                    .filter(fileType -> fileType.getContentType().equals(contentType))
                    .findFirst()
                    .orElse(null);
        }
        return null;
    }

    /**
     * 所有文件类型是否包含传入的文件类型
     *
     * @param contentType 文件类型
     * @return true: 包含 / false: 不包含
     * @author tianyi.jiang
     * @since 1.0
     */
    public static boolean contains(String contentType) {
        return getFileTypeEnum(contentType) != null;
    }

    /**
     * 限定文件类型是否包含传入的文件类型
     *
     * @param contentType 文件类型
     * @return true: 包含 / false: 不包含
     * @author tianyi.jiang
     * @since 1.0
     */
    public static boolean contains(FileTypeEnum[] fileTypeEnums, String contentType) {
        if (ArrayUtils.isNotEmpty(fileTypeEnums) && StringUtils.isNotEmpty(contentType)) {
            return Stream.of(fileTypeEnums)
                    .anyMatch(fileType -> fileType.getContentType().equals(contentType));
        }
        return false;
    }

}